package com.jumi.microservice.stock.service.impl;

import com.jumi.microservice.common.utils.StringProvider;
import com.jumi.microservice.material.dto.JmUnitResponse;
import com.jumi.microservice.material.dto.category.JmCategoryResponse;
import com.jumi.microservice.material.dto.supplier.JmSupplierResponse;
import com.jumi.microservice.stock.dto.StockGoodsRequest;
import com.jumi.microservice.stock.dto.StockGoodsResponse;
import com.jumi.microservice.stock.mapper.StockGoodsMapper;
import com.jumi.microservice.stock.service.CommonService;
import com.jumi.microservice.stock.service.StockGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName StockGoodsServiceImpl
 * @Description
 * @Author Abel Yang
 * @Date 2020/8/19/019 15:19
 **/
@Service
public class StockGoodsServiceImpl implements StockGoodsService {
    @Resource
    private CommonService commonService;
    @Resource
    private StockGoodsMapper jmStockGoodsMapper;

    /**
     * @Description: 查询商品库存列表
     * @Param jmStockGoodsRequest
     * @Return {@link List<  StockGoodsResponse >}
     * @Throws
     * @Author Abel Yang
     * @Date 2020/8/22/022 11:52
     */
    @Override
    public List<StockGoodsResponse> getStockGoodsList(StockGoodsRequest jmStockGoodsRequest) {
        List<StockGoodsResponse> stockGoodsList = jmStockGoodsMapper.findAll(jmStockGoodsRequest);
        if (stockGoodsList.isEmpty()) {
            return stockGoodsList;
        }
        return formatStockGoodsList(stockGoodsList);
    }

    /**
     * @Description: 格式化商品仓库列表
     * @Param stockGoodsList
     * @Return {@link List<  StockGoodsResponse >}
     * @Throws
     * @Author Abel Yang
     * @Date 2020/8/22/022 14:05
     */
    private List<StockGoodsResponse> formatStockGoodsList(List<StockGoodsResponse> stockGoodsList) {

        //查询商品分类
        List<Long> categoryIds = stockGoodsList.stream().map(StockGoodsResponse::getCateId).distinct().collect(Collectors.toList());
        Map<Long, JmCategoryResponse> categoryMap = commonService.getCategoryMapByIds(categoryIds);

        //查询商品单位
        Map<Integer, JmUnitResponse> unitMap = commonService.getUnitMapByIds(stockGoodsList.stream().map(StockGoodsResponse::getUnitId).distinct().collect(Collectors.toList()));

        //查询商品供应商
        Map<Long, JmSupplierResponse> supplierMap = commonService.getSupplierMapByIds(stockGoodsList.stream().map(StockGoodsResponse::getSupplierId).distinct().collect(Collectors.toList()));

        stockGoodsList.forEach(stock -> {
            JmCategoryResponse category = null != categoryMap.get(stock.getCateId()) ? categoryMap.get(stock.getCateId()) : new JmCategoryResponse();
            stock.setCateName(StringProvider.isNotEmpty(category.getCategoryName()) ? category.getCategoryName() : "");

            JmUnitResponse unitInfo = null != unitMap.get(stock.getUnitId()) ? unitMap.get(stock.getUnitId()) : new JmUnitResponse();
            stock.setUnit(StringProvider.isNotEmpty(unitInfo.getSmunitName()) ? unitInfo.getSmunitName() : "");

            JmSupplierResponse supplier = null != supplierMap.get(stock.getSupplierId()) ? supplierMap.get(stock.getSupplierId()) : new JmSupplierResponse();
            stock.setSupplierCompanyName(StringProvider.isNotEmpty(supplier.getSupplierCompanyName()) ? supplier.getSupplierCompanyName() : "");
        });

        return stockGoodsList;
    }
}
